<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\Payment\Mapper;

use App\Payment\Model\PaymentMethod;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 支付配置表Mapper类
 */
class PaymentMethodMapper extends AbstractMapper
{
    /**
     * @var PaymentMethod
     */
    public $model;

    public function assignModel()
    {
        $this->model = PaymentMethod::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 支付名称
        if (isset($params['title']) && filled($params['title'])) {
            $query->where('title', 'like', '%'.$params['title'].'%');
        }

        // 支付图标
        if (isset($params['icon']) && filled($params['icon'])) {
            $query->where('icon', 'like', '%'.$params['icon'].'%');
        }

        // 设备 1web 2android 3ios,4.h5
        if (isset($params['device']) && filled($params['device'])) {
            $query->where('device', '=', $params['device']);
        }

        // 支付方式 80001支付宝 80002微信 80003云闪付 80004银行卡80005 ios
        if (isset($params['method']) && filled($params['method'])) {
            $query->where('method', '=', $params['method']);
        }

        // 渠道名称
        if (isset($params['channel_name']) && filled($params['channel_name'])) {
            $query->where('channel_name', 'like', '%'.$params['channel_name'].'%');
        }

        // 渠道类型
        if (isset($params['channel_type']) && filled($params['channel_type'])) {
            $query->where('channel_type', 'like', '%'.$params['channel_type'].'%');
        }

        // 排序
        if (isset($params['sort']) && filled($params['sort'])) {
            $query->where('sort', '=', $params['sort']);
        }

        // 状态 1可用0不可用
        if (isset($params['status']) && filled($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 渠道配置
        if (isset($params['setting']) && filled($params['setting'])) {
            $query->where('setting', '=', $params['setting']);
        }

        // 是否默认1是0否
        if (isset($params['is_default']) && filled($params['is_default'])) {
            $query->where('is_default', '=', $params['is_default']);
        }

        // 支付超时时间
        if (isset($params['timeout']) && filled($params['timeout'])) {
            $query->where('timeout', '=', $params['timeout']);
        }

        // 创建时间
        if (isset($params['create_time']) && filled($params['create_time'])) {
            $query->where('create_time', '=', $params['create_time']);
        }

        // 更新时间
        if (isset($params['update_time']) && filled($params['update_time'])) {
            $query->where('update_time', '=', $params['update_time']);
        }

        // 删除时间
        if (isset($params['delete_time']) && filled($params['delete_time'])) {
            $query->where('delete_time', '=', $params['delete_time']);
        }

        // 创建人
        if (isset($params['created_by']) && filled($params['created_by'])) {
            $query->where('created_by', '=', $params['created_by']);
        }

        return $query;
    }
    public function saveConfig($params)
    {
        return $this->model::query()->where('id', $params['id'])->update(['setting' => json_encode($params)]) ? [] : [];
    }

    public function typeList($params){
        $returnData=[];
        switch ($params['method']){
            case 80001:
                $returnData=[
                    ['label'=>'支付宝H5','value'=>'alipay_h5'],
                    ['label'=>'支付宝WEB','value'=>'alipay_web'],
                    ['label'=>'支付宝APP','value'=>'alipay_app'],

                ];
                break;
            case 80002:
                $returnData=[
                    ['label'=>'微信H5','value'=>'weixin_h5'],
                    ['label'=>'微信WEB','value'=>'weixin_web'],
                    ['label'=>'微信APP','value'=>'weixin_app']
                ];
                break;
            case 80005:
                $returnData=[['label'=>'苹果支付','value'=>'ios_app']];
                break;
            case 80006:
                $returnData=[['label'=>'银行卡扫码','value'=>'heepay_web']];
                break;
            case 80008:
                $returnData=[
                    ['label'=>'支付宝扫码','value'=>'fuiou_alipay'],
                    ['label'=>'微信扫码','value'=>'fuiou_weixin']
                ];
                break;
            case 80009:
                $returnData=[
                    ['label'=>'支付宝wap','value'=>'hlb_wap_alipay'],
                    ['label'=>'支付宝扫码','value'=>'hlb_qrcode_alipay'],
                    ['label'=>'微信扫码','value'=>'hlb_qrcode_weixin'],
                    ['label'=>'微信jspay','value'=>'hlb_jspay_weixin'],
                    ['label'=>'微信wap','value'=>'hlb_wap_weixin']
                ];
                break;
        }
        return $returnData;
    }
}